package com.quartertone.summingcells;

import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class SuummingCells extends Activity implements OnClickListener {

	private static final int[] numcolors = new int[] {
		Color.parseColor("#666666"),
		Color.parseColor("#cc0033"),
		Color.parseColor("#ff0000"),
		Color.parseColor("#ff9900"),
		Color.parseColor("#ffff00"),
		Color.parseColor("#00aa00"),
		Color.parseColor("#00ffff"),
		Color.parseColor("#ff00cc"),
		Color.parseColor("#999999"),
		Color.parseColor("#ffffff"),
		Color.parseColor("#cc8833"), // this is the playground color
	};

	public int[][] boxvalues = new int[10][10];
	public HashMap<String, int[]> boxhash = new HashMap<String, int[]>();
	public View grid;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = getLayoutInflater();
		View grid = inflater.inflate(R.layout.main, (ViewGroup) findViewById(R.id.GameBoard));

		// Generate the colored tiles
		Random rand = new Random();
		int x; int y; int colornum;
		for (x=2;x<=8;x++) {
			for (y=2;y<=8;y++) {
				colornum = rand.nextInt(10);
				makegridBox(grid, x, y, colornum);
				pushHash(x,y,colornum);
	
			}			
		} // end generate color
		// Button-ize the other tiles
		for (y=1;y<=9;y++) {
			// do first and last row
			// (1,y) and (9,y)
			clearBlock(grid, 1,y);
			pushHash(1,y,0);
			clearBlock(grid, 9,y);
			pushHash(9,y,0);
		}
		for (x=2;x<=8;x++) {
			// do first and last column, minus first and last rows
			// (x,1) and (x,9)
			clearBlock(grid, x, 1);
			pushHash(x,1,0);
			clearBlock(grid, x, 9);
			pushHash(x,9,0);
		}

		setContentView(grid);
	}

	
	public void pushHash (int x, int y, int colornum) {
		// here we populate box hash for later
		int initboxID; int[] boxcoord = new int[2];
		boxvalues[x][y] = colornum;
		boxcoord[0] = x; boxcoord[1] = y;
		System.out.println("" + boxcoord[0] + ", " + boxcoord[1]);
		initboxID = getResources().getIdentifier("box" + Integer.toString(getboxnum(x,y)), "id", "com.quartertone.summingcells");
		boxhash.put(Integer.toString(initboxID), boxcoord);
	}

	@Override
	public void onClick(View v) {
		//buttonPressed();
		v.setBackgroundColor(Color.GRAY);
		//int[] boXY = boxhash.get(v.getId());
		//clearBlock(grid, boXY[0]+1,boXY[1]+1);
		EditText ta = ((EditText) findViewById(R.id.textarea));
		ta.setText(Integer.toString(v.getId()));
	//	System.out.println(boxhash.get(Integer.toString(v.getId())));

		int[] thisbox = boxhash.get(Integer.toString(v.getId()));
		ta.append("=" + thisbox[0] + ", " + thisbox[1] + "\n");
		
		// find surrounding boxes and add values
		int x; int y; int sumofboxes = 0;
		for (x = thisbox[0]-1; x<=thisbox[0]+1; x++) {
			if (x<1 || x>9) continue;
			for (y = thisbox[1]-1; y<=thisbox[1]+1; y++) {
				if (x==thisbox[0] && y==thisbox[1]) continue;
				if (y<1 || y>9) continue;
				sumofboxes += boxvalues[x][y];
				ta.append("" + x + ", " + y + " | ");
				System.out.println(boxhash.get(Integer.toString(v.getId())));
			}
		}
		ta.append("\n" + Integer.toString(sumofboxes));
	//	textarea.setText("ahore"); //Integer.toString(v.getId()));
	//	toastIt(Integer.toString(v.getNextFocusDownId()));
	}

	public int getboxnum(int x, int y) {
		return Integer.valueOf(Integer.toString(x) + Integer.toString(y));
	}

	
	public void toastIt(CharSequence text) {
		Context context = getApplicationContext();
		int duration = Toast.LENGTH_SHORT;
		Toast.makeText(context, text, duration).show();
	}
	
	
	
	public View checkMyBox (View grid, int x, int y) {
		int boxnum = getboxnum(x,y);
		int boxID = getResources().getIdentifier("box" + Integer.toString(boxnum), "id", "com.quartertone.summingcells");
		View box = (View) grid.findViewById(boxID);
		//box.setFocusableInTouchMode(true);
		return box;

	}

	public void clearBlock (View grid, int x, int y) {
		View box = checkMyBox(grid, x, y);
		box.setOnClickListener(this);
/*		box.setOnClickListener(
				new View.OnClickListener() {
					@Override public void onClick(View arg0) {
						textarea.setText("haha");
					}
				});
				*/
		box.setBackgroundColor(numcolors[10]);
	}

	public void makegridBox (View grid, int x, int y, int color) {
		View box = checkMyBox(grid, x, y);
		box.setOnClickListener(this);
		box.setBackgroundColor(numcolors[color]); //Color.GRAY);

	}


} // this is the END

